/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.service;

import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.service.CrudService;
import cn.mcsys.myadmin.common.utils.Constant;
import cn.mcsys.myadmin.common.utils.DateUtils;
import cn.mcsys.myadmin.modules.air.dao.AirRidePointDao;
import cn.mcsys.myadmin.modules.air.entity.AirCar;
import cn.mcsys.myadmin.modules.air.entity.AirCarRecord;
import cn.mcsys.myadmin.modules.air.entity.AirRealtimeMessage;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.vo.AirRidePointVO;
import cn.mcsys.myadmin.modules.viewer.service.AirRideQRCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 乘车点管理Service
 *
 * @author leopold
 * @version 2018-08-13
 */
@Service
@Transactional(readOnly = true)
public class AirRidePointService extends CrudService<AirRidePointDao, AirRidePoint> {

    @Autowired
    private AirCarService airCarService;

    @Autowired
    private AirDriverService airDriverService;

    @Autowired
    private AirSendMessageService airSendMessageService;

    @Autowired
    private AirRealtimeMessageService airRealtimeMessageService;

    @Autowired
    private AirCarRecordService airCarRecordService;

    @Autowired
    AirRideQRCodeService airRideQRCodeService;

    public AirRidePoint get(Integer id) {
        return super.get(id);
    }

    public List<AirRidePoint> findList(AirRidePoint airRidePoint) {
        return super.findList(airRidePoint);
    }

    public Page<AirRidePoint> findPage(Page<AirRidePoint> page, AirRidePoint airRidePoint) {
        return super.findPage(page, airRidePoint);
    }

    public List<AirRidePoint> findAllList() {
        return dao.getAllList();
    }

    @Transactional(readOnly = false)
    public void save(AirRidePoint airRidePoint) {
        super.save(airRidePoint);
    }

    @Transactional(readOnly = false)
    public void updateTemplate(AirRidePoint airRidePoint) {
        dao.update(airRidePoint);
    }

    @Transactional(readOnly = false)
    public void delete(AirRidePoint airRidePoint) {
        super.delete(airRidePoint);
    }

    /**
     * 查询所有服务站点和对应的所有的车
     *
     * @return
     */
    public Map<String, Object> selectAllPoint() {
        Map<String, Object> map = new HashMap<>();

        List<AirRidePointVO> ridePointVOS = new ArrayList<>();
        List<AirRidePointVO> emergencyLogVOS = new ArrayList<>();
        List<AirRidePoint> points = dao.findAllList(new AirRidePoint());
        for (AirRidePoint point : points) {
            AirRidePointVO vo = new AirRidePointVO();
            setPointDispatchVO(vo, point);
            if (point.getAirParkingLot() != null) {
//		        乘车点
                ridePointVOS.add(vo);
            } else {
//		        应急停车场
                emergencyLogVOS.add(vo);
            }
        }
        map.put("rps", ridePointVOS);
        map.put("ems", emergencyLogVOS);
        return map;
    }

    public List<AirRidePointVO> findAllAppointList(){
        List<AirRidePointVO> ridePointVOS = new ArrayList<>();
        List<AirRidePoint> allAppointedList = dao.findAllAppointedList();
        for (AirRidePoint airRidePoint : allAppointedList) {
            AirRidePointVO vo = new AirRidePointVO();
            setPointVO(vo, airRidePoint);
            if (airRidePoint.getAirParkingLot() != null) {
                ridePointVOS.add(vo);
            }
        }
        return ridePointVOS;
    }

    private void setPointVO(AirRidePointVO vo, AirRidePoint point) {
        List<AirCar> airCars = airCarService.selectByPointId(point.getId());
        int seatingSum = 0;
        int takerSum = 0;
        for (AirCar airCar : airCars) {
            seatingSum += airCar.getSeating();
            if(airCar.getStatus()!=null) {
                if (airCar.getStatus() == 1) {
                    takerSum += airCar.getSeating();
                }
            }
        }
        vo.setCars(airCars);
        vo.setSeatingSum(seatingSum);
        vo.setTakerSum(takerSum);
        vo.setRidePoint(point);

    }

    private void setPointDispatchVO(AirRidePointVO vo, AirRidePoint point) {
        List<AirCar> airCars = airCarService.selectByPointId(point.getId());
        int seatingSum = 0;
        for (AirCar airCar : airCars) {
            if(airCar.getSeating()!=null) {
                seatingSum += airCar.getSeating();
            }
        }
        vo.setCars(airCars);
        vo.setSeatingSum(seatingSum);
        vo.setRidePoint(point);

    }

    /**
     * 更新移动车辆
     *
     * @param carIds
     * @param pointId
     */
    @Transactional
    public void moveCarUpdate(Long[] carIds, Long pointId) {
//        乘车点的司机数和车辆数要增加
        updatePointCarSum(pointId, carIds);
        airCarService.update4pointId(carIds, pointId.intValue());
    }


    /**
     * 车辆调度页面的新增车辆
     *
     * @param cars
     * @param emergencyCars
     * @param carPointId
     * @param emergencyPointId
     */
    @Transactional(readOnly = false)
    public void addPointCarSave(Long[] cars, Long[] emergencyCars, Long carPointId, Long emergencyPointId) {
//		更新乘车点的车辆数和司机数
        updatePointCarSum(carPointId, cars);
//        更新应急停车场车辆数量和司机数
        updatePointCarSum(emergencyPointId, emergencyCars);
//    	更新车辆的乘车点
        airCarService.update4pointId(cars, carPointId.intValue());
//      更新应急车辆
        airCarService.update4pointId(emergencyCars, emergencyPointId.intValue());
    }

    /**
     * 更新当前站点的车辆数
     *
     * @param pointId
     * @param cars
     */
    private void updatePointCarSum(Long pointId, Long[] cars) {
        AirRidePoint airRidePoint = dao.get(pointId.intValue());
//        cars包含已有的
        List<AirCar> airCars = airCarService.selectByIds(cars);
        int drivers = 0;
        for (AirCar airCar : airCars) {
            //如果现在的车辆原来就不是属于选中的乘车点,
//            String note = airRidePoint.getName() + Constant.ADDCAR + airCar.getCarNumber();
//            airRealtimeMessageService.setProperty(airRidePoint, AirRealtimeMessage.SCHEDULING, note);
            AirRidePoint oldPoint = get(airCar.getAirRidePoint().getId());
            oldPoint.setBusSum(oldPoint.getBusSum() - 1);
            if (airCar.getDriver() != null) {
                oldPoint.setDriverSum(oldPoint.getDriverSum() - 1);
                drivers += 1;
            }

            dao.update(oldPoint);
            if (airCar.getAirRidePoint().getId() == pointId.intValue()){
                airRidePoint = oldPoint;
            }

            //发送APP通知
            String message = airCar.getAirRidePoint().getName()+Constant.arror+airRidePoint.getName()+Constant.MOVECAR + airCar.getCarNumber();
            airSendMessageService.sendToWorker(message,oldPoint.getId(),airRidePoint.getId());
            airRealtimeMessageService.setProperty(oldPoint, AirRealtimeMessage.SCHEDULING, message);
            airRealtimeMessageService.setProperty(airRidePoint, AirRealtimeMessage.SCHEDULING, message);
        }
        //更新新站点的车辆
        airRidePoint.setBusSum(airRidePoint.getBusSum() + cars.length);
        airRidePoint.setDriverSum(airRidePoint.getDriverSum() + drivers);
        dao.update(airRidePoint);
    }

    public Integer getSeatById(Integer id) {
        List<AirCar> cars = airCarService.getPointCars(id.longValue());
        Integer seats =0;
        for (AirCar car : cars) {
            seats += car.getSeating();
        }
        System.out.println("-----------------------------"+seats+"----------------");
        return seats;
    }

    public List<AirRidePoint> findAllAppointedList() {
        return dao.findAllAppointedList();
    }

    @Transactional(readOnly = false)
    public void updateWaitingSum(Integer pointId, Integer sum) {
        dao.updateWaitingSum(pointId,sum);
    }

    public Map<String,Object> getCountById(Integer id) {
        Map<String,Object> result = new HashMap<>();
        Date date1 = DateUtils.parseDate("2018-11-9");
        Date date2 = DateUtils.parseDate("2018-11-10");
        Date date3 = DateUtils.parseDate("2018-11-11");
        List<AirCarRecord> records1 = airCarRecordService.getRecordsByPointIdAndDate(id, date1);
        List<AirCarRecord> records2 = airCarRecordService.getRecordsByPointIdAndDate(id, date2);
        List<AirCarRecord> records3 = airCarRecordService.getRecordsByPointIdAndDate(id, date3);
        result.put("took1",records1.size());
        result.put("took2",records2.size());
        result.put("took3",records3.size());

        Integer appoint1 = airRideQRCodeService.selectCountByPointId(id, "2018-11-9");
        Integer appoint2 = airRideQRCodeService.selectCountByPointId(id, "2018-11-10");
        Integer appoint3 = airRideQRCodeService.selectCountByPointId(id, "2018-11-11");
        if(appoint1==null){
            appoint1=0;
        }
        if(appoint2==null){
            appoint2=0;
        }
        if(appoint3==null){
            appoint3=0;
        }
        result.put("appoint1",appoint1);
        result.put("appoint2",appoint2);
        result.put("appoint3",appoint3);

        return result;
    }

    public boolean RouteIsBind(Integer id, Integer route) {
        if(route>0) {
            AirRidePoint ridePoint = dao.getByRoute(route);
            if(ridePoint!=null) {
                if (ridePoint.getId().intValue() == id.intValue()) {
                    return false;
                }
            }else {
                return false;
            }
        }
        return true;
    }

    public List<AirRidePoint> selectByPointType(Integer type) {
        return dao.selectByPointType(type);
    }

    public List<AirRidePoint> findAllPointList() {
        return dao.getAllPointList();
    }
}